home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH2 / ParedFruit.cs < prev    next >
Text File  |  2006-05-29  |  2KB  |  67 lines

  1. // ========================================================================
  2. //  ParedFruit.cs
  3. //
  4. //  One solution to the optimization challenge
  5. //  this rolls all loops into one loop
  6. //
  7. // ========================================================================
  8.  
  9. function runParedFruit()
  10. // ------------------------------------------------------------------------
  11. //     Entry point for program. This program adds up the costs
  12. //     and quantities of selected fruit types and outputs the results to
  13. //     the display. This program is a variation of the program FruitLoopy
  14. //
  15. // ------------------------------------------------------------------------
  16. {
  17.  //
  18.  // ----------------- Initialization ---------------------
  19.  //
  20.  
  21.  %numFruitTypes = 5; // so we know how many types are in our arrays
  22.  
  23.  %bananaIdx=0;    // initilize the values of our index variables
  24.  %appleIdx=1;
  25.  %orangeIdx=2;
  26.  %mangoIdx=3;
  27.  %pearIdx=4;
  28.  
  29.  %names[%bananaIdx] = "bananas"; // initilize the fruit name values
  30.  %names[%appleIdx] = "apples";
  31.  %names[%orangeIdx] = "oranges";
  32.  %names[%mangoIdx] = "mangos";
  33.  %names[%pearIdx] = "pears";
  34.  
  35.  %cost[%bananaIdx] = 1.15; // initilize the price values
  36.  %cost[%appleIdx] = 0.55;
  37.  %cost[%orangeIdx] = 0.55;
  38.  %cost[%mangoIdx] = 1.90;
  39.  %cost[%pearIdx] = 0.68;
  40.  
  41.  %quantity[%bananaIdx] = 1; // initilize the quantity values
  42.  %quantity[%appleIdx]  = 3;
  43.  %quantity[%orangeIdx] = 4;
  44.  %quantity[%mangoIdx]  = 1;
  45.  %quantity[%pearIdx]   = 2;
  46.  
  47.  %numFruit=0;     // always a good idea to initialize *all* variables!
  48.  %totalCost=0;    // (even if we know we are going to change them later)
  49.  
  50.  //
  51.  // ----------------- Computation ---------------------
  52.  //
  53.  
  54.  // Display the known statistics of the fruit collection
  55.  // count up all the pieces of fruit and calculate the total cost
  56.  for (%index = 0; %index < %numFruitTypes; %index++)
  57.  {
  58.    echo("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
  59.    echo("Number of " @ %names[%index] @ ":" @ %quantity[%index]);
  60.    %numFruit = %numFruit + %quantity[%index];
  61.    %totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
  62.  }
  63.  // now echo the totals
  64.  echo("Total pieces of Fruit:" @ %numFruit);
  65.  echo("Total Price of Fruit:$" @ %totalCost);
  66. }
  67.